/* -----------------------------------------------------------------------
 * Include the following lines in your makefile:
 *
 * unittest1: cardtest4.c dominion.o rngs.o
 *      gcc -o cardtest4 -g  cardtest4.c dominion.o rngs.o $(CFLAGS)
 * -----------------------------------------------------------------------
 */

#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"

// set NOISY_TEST to 0 to remove printfs from output
#define NOISY_TEST 1

// set USE_ASSERT to 1 to include the assert statement
#define USE_ASSERT 0

#define PLAY_CARD steward
#define PLAY_CARD_STR "steward"

int main() {
    int c, r, handCount = 5, handPos = 0, choice1 = 0, choice2 = 1, choice3 = 2;
    int numPlayer = 2;
    int seed = 1000;
    int k[10] = {adventurer, council_room, feast, gardens, mine
               , remodel, smithy, village, baron, great_hall};
    struct gameState G;
    int maxHandCount = 5;
    int cardChange = 0;
    int coinChange = 0;
    int player_hand[maxHandCount];
    int preHandCount, preCoins, preDiscard;
    int postHandCount, postCoins, postDiscard;

	player_hand[0] = PLAY_CARD;
	player_hand[1] = copper;
	player_hand[2] = copper;
	player_hand[3] = estate;
	player_hand[4] = estate;

#if (NOISY_TEST == 1)    		
    printf ("TESTING %s card:\n", PLAY_CARD_STR);	
#endif

	//Test card choices 0 through 2
	printf("choice 0 = trash two cards (coppers)\n");
	printf("choice 1 = draw two cards (coppers)\n");
	printf("choice 2 = add two coins\n");
	
	for (c = 0; c < 3; c++)
    {
#if (NOISY_TEST == 1)
		printf("Test choice = %d.\n", c);
#endif
		memset(&G, 23, sizeof(struct gameState));   // clear the game state
	    r = initializeGame(numPlayer, k, seed, &G); // initialize a new game
		G.handCount[0] = handCount;                 // set the number of cards on hand
		memcpy(G.hand[0], player_hand, sizeof(int) * handCount); // set all the cards to player_hand
		preHandCount = G.handCount[0];
		preDiscard = G.discardCount[0];
		preCoins = 2;
		G.coins = preCoins;
		
		//Set preEstateCount and preCoins based on player_hand
		switch (c){
		case 0:
			cardChange = -2;
			// Trash two coppers;
			coinChange = -2;
			break;			
		case 1:
			cardChange = 2;
			// Draw two coppers;
			coinChange = 2;
			break;
		case 2: 
			cardChange = 0;
			coinChange = 2;
			break;
		default:
			break;
		}
		
		// play the card in position 0
		choice1 = c;		
		r = playCard(handPos,choice1,choice2,choice3,&G);
		postHandCount = G.handCount[0];
		postDiscard = G.discardCount[0];
		postCoins = G.coins;

#if (NOISY_TEST == 1)
        printf("Pre hand count = %d, Post hand count = %d, expected = %d.\n", preHandCount, postHandCount, preHandCount + cardChange - 1);
        printf("Pre coins = %d, Post coins = %d, expected = %d.\n", preCoins, postCoins, preCoins + coinChange);
#endif					
		
		assert(postHandCount == preHandCount + cardChange - 1);

#if (USE_ASSERT == 1)			
		// The steward should add 2 coins if the player chooses option 2.
		assert(postCoins == preCoins + coinChange);
#endif		

	}
	
#if (USE_ASSERT == 0)
		printf("Did not test assert of coins.\n");
#endif
    
    printf("All domain tests passed!\n");
        
    return 0;
}
